#include "StreamByte.h"
#include <utility>

DEF_BEG_LIBTOOLS

CStreamIteator::CStreamIteator() :_beg(NullPtr), _end(NullPtr)
{

}

CStreamIteator::CStreamIteator(char* b, char* e) : _beg(b), _end(e)
{

}

size_t CStreamIteator::size() const
{
	return (_end - _beg);
}

const char* CStreamIteator::cbegin() const 
{
	return _beg;
}

const char* CStreamIteator::cend() const
{
	return _end;
}

char* CStreamIteator::begin()
{
	return _beg;
}

char* CStreamIteator::end()
{
	return _end;
}

CStreamBuffer::CStreamBuffer() :_data(NullPtr), _size(0), _max(0)
{
}

CStreamBuffer::CStreamBuffer(size_t size) :_data(NullPtr), _size(0), _max(0)
{
	assign(size);
}

CStreamBuffer::CStreamBuffer(const CStreamBuffer& st) : _data(NullPtr), _size(0), _max(0)
{
	*this = st;
}

#ifdef STD11
CStreamBuffer::CStreamBuffer(CStreamBuffer&& st) :_data(NullPtr), _size(0), _max(0)
{
	*this = std::move(st);
}
#endif

CStreamBuffer::~CStreamBuffer()
{
	clear();
}

void CStreamBuffer::clear()
{
	if (_data){
		delete[] _data;
	}
	 _data = NullPtr;
	 _size = 0;
	 _max = 0;
}

void CStreamBuffer::assign(size_t size)
{
	if (_max < size)
	{
		if (_data) {
			delete[] _data;
		}
		_data = new char[size];
		_max = size;
	}
	_size = size;
}

size_t CStreamBuffer::capacity() const
{
	return _max;
}

void CStreamBuffer::reserve(size_t size)
{
	if (_data) {
		delete[] _data;
	}
	 _data = new char[size];
	 _max = size;
	 _size = size;
}

char* CStreamBuffer::data()
{
	return _data;
}

char* CStreamBuffer::begin()
{
	return _data;
}

const char* CStreamBuffer::cbegin() const
{
	return (const char*)_data;
}

char* CStreamBuffer::end()
{
	return _data + _size;
}

const char* CStreamBuffer::cend() const
{
	return (const char*)(_data + _size);
}

size_t CStreamBuffer::size() const
{
	return _size;
}

char& CStreamBuffer::operator[] (int pos)
{
	return _data[pos];
}

char CStreamBuffer::operator[] (int pos) const
{
	char ch = _data[pos];
	return ch;
}

CStreamBuffer& CStreamBuffer::operator= (const CStreamBuffer& st)
{
	if (this != &st){
		copy_mem(st._data, st.size());
	}
	return *this;
}

#ifdef STD11
CStreamBuffer& CStreamBuffer::operator= (CStreamBuffer&& st)
{
	if (this != &st)
	{
		clear();
		std::swap(_data, st._data);
		std::swap(_size, st._size);
		std::swap(_max, st._max);
	}

	return *this;
}
#endif // STD11

void CStreamBuffer::swap(CStreamBuffer& st)
{
	std::swap(_data, st._data);
	std::swap(_size, st._size);
	std::swap(_max, st._max);
}

const void* CStreamBuffer::copy_mem(const void* dst, size_t size)
{
	assign(size);
	memcpy(_data, dst, size);
	return dst;
}

DEF_END_LIBTOOLS